package com.bishe.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.StrUtil;
import com.api.EcoBootException;
import com.api.Result;
import com.api.entitis.FileInfo;
import com.api.entitis.Resume;
import com.api.entitis.query.ResumeQuery;
import com.api.service.IFileService;
import com.api.service.IResumeService;
import com.api.util.JobUtils;
import com.api.util.UserStpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

/**
 * @author 简历
 * @date 2022/2/17 16:27
 */
@RestController
@CrossOrigin
@RequestMapping("/resume")
@Api(tags = "简历控制器")
public class ResumeController {
    @Autowired
    private IResumeService resumeService;

    @Autowired
    private IFileService fileService;

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加和修改简历")
    @SaCheckLogin(type = UserStpUtil.TYPE)  //检查用户登录
    public Result addOrUpdate(@RequestBody Resume resume) {
        if (resume == null) {
            return Result.error("resume 为空！");
        }
        if (StrUtil.isEmpty(resume.getTitle()) ||
                StrUtil.isEmpty(resume.getContent()) ||
                StrUtil.isEmpty(resume.getType())
        ) {
            return Result.error("简历内容为空！");
        }
        resumeService.addOrUpdate(resume);

        return Result.ok("ok", resume.getId());
    }


    @GetMapping("/getResume")
    @ApiOperation("获取简历")
    @SaCheckLogin(type = UserStpUtil.TYPE)  //检查用户登录
    public Result getResume() {
        Resume resume = resumeService.getResume();
        return Result.ok(resume);
    }

    @ApiOperation("上传简历")
    @PostMapping("/upload")
    public Result upload(MultipartFile file, String dataId) {

        //如果有文件则删除
        List<FileInfo> list = fileService.list(new LambdaQueryWrapper<FileInfo>()
                .eq(FileInfo::getCreateBy, JobUtils.getLoginId()));
        //遍历删除
        list.forEach(s -> {
            fileService.deleteFile(s.getId());
        });

        //上传文件
        fileService.upload(file, dataId);

        return Result.ok("上传成功");
    }

    @GetMapping("/toPage")
    @ApiOperation("简历信息列表")
    public Result toPage(ResumeQuery query) {
        IPage<Resume> resumeIPage = resumeService.toPage(query);
        return Result.ok(resumeIPage);
    }


    @GetMapping("/getData/{id}")
    @ApiOperation("简历详情")
    public Result getData(@PathVariable String id) {

        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        Resume resume = resumeService.getDetail(id);
        if (resume == null){
            return null;
        }
        return Result.ok(resume);
    }

}
